// 云函数入口文件
const cloud = require('wx-server-sdk')
const CryptoJS = require('./crypto')
cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {

    return new Promise((resolve, reject) => {
        var pd = event.getPd
        var decodedData = event.getContent
        //揭秘
        var keyHex = CryptoJS.enc.Hex.parse(pd);
        var decrypted = CryptoJS.AES.decrypt({
            ciphertext: CryptoJS.enc.Hex.parse(decodedData)
        }, keyHex, {
            iv: [],
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        var decryptedData = decrypted.toString(CryptoJS.enc.Hex);
        var setOnePart = decryptedData.substring(0,8)
        var setTwoPart = decryptedData.substring(8,12)
        var setThreePart = decryptedData.substring(12,16)
        var setFourPart = decryptedData.substring(16,20)
        var setFivePart = decryptedData.substring(20,32)
        var newResult = setOnePart + "-" + setTwoPart+ "-"+ setThreePart+ "-"+ setFourPart+ "-"+ setFivePart
        resolve(newResult)
      })


}
